// 导出和打印报表专用接口
// import axios from '@/libs/api.request';
// import config from '@/config';
import { DevPrefix } from '@/config/cfg.js';

import { getToken } from '@/libs/util';
// console.log(process.env.NODE_ENV, '本地环境？');
// const baseUrl = config.baseUrl.rqDev;
//  process.env.NODE_ENV === 'development' ? 'http' + DevPrefix : '';
//     ? config.baseUrl.dev
//     : config.baseUrl.pro;
// const baseUrl = `//39.99.227.207`;
// const baseUrl = `://qa.ccnc.cc/qaapi/`;
const baseUrl = DevPrefix;
const apiObj = {
  initList (list, fullcode) {
    // console.log('打印前准备list：', list);
    let result = '';
    if (fullcode.toString() !== 'false') { // fullcode是樊志林的评定表，链接中需要有fullcode和reportId字段
      list.forEach(element => {
        result += `%7B/view/${element.filename}_view.rpx(arg1=${element.dataId};pid=${element.nodeId};token=${getToken()};tid=1;fenxiang_id=${element.nodeId};fullcode=${element.fullcode};reportId=${element.reportId || element.id})%7D`;
      });
    } else { // 否则就是孟凯的检查表，链接中只需要增加reportId字段
      list.forEach(element => {
        // console.log(element, 'element');
        // console.log(element.reportId, 'reportId');
        // console.log(`${element.reportId || element.id}`, '====');
        result += `%7B/view/${element.filename}_view.rpx(arg1=${element.dataId};pid=${element.nodeId},${element.reportId || element.id};token=${getToken()};tid=1)%7D`;
      });
    }

    return result;
  },
  // 打印报表接口
  printR (type, list, fullcode) {
    let url = '';
    url = `${baseUrl}/${type}/reportJsp/pdfBatchPrint.jsp?report=${this.initList(
      list, fullcode
    )}`;
    console.log(url, '打印');
    window.open(url, '_blank');
  },
  // 导出报表接口
  exportR (type, list, pdf, fullcode) {
    // 区别pdf打印和excel打印
    let url = '';
    const group = 5;// 默认批量五个一组导出
    const len = list.length;
    const result = [];
    for (let i = 0; i < len; i += group) {
      result.push(list.slice(i, i + group)); // 每5项分成一组
    }
    result.forEach(element => {
      url = `${baseUrl}/${type}/reportJsp/${
        pdf ? 'exportPdf' : 'exportExcel'
      }.jsp?report=${this.initList(element, fullcode)}`;
      console.log(url, '导出');
      // return;

      window.open(url, '_blank');
    });
  }
};

export default apiObj;
